import { relationalStore } from "@kit.ArkData"
import { Field, FieldType } from "../Field"
import { Table } from "../Table"

export class BdUserEntity {
  id?: number = 0
  username: string = ""
  email: string = ""
  password: string = ""
  phone: string = ""


  static fieldsName: string [] = [];


  static initClass() {
    //如果属性没有默认值,其key获取不到.
    const obj = new BdUserEntity();
    this.fieldsName = Object.getOwnPropertyNames(obj);
  }

  /**
   * @deprecated
   * create的valueBucket不应该从entity中取.
   * entity应该是一个完整的数据.
   * @returns
   */
  getValueBucket(): relationalStore.ValuesBucket {
    const bucket = {};
    BdUserEntity.fieldsName.forEach(prop => {
      if (this[prop] === undefined) {
        return
      }
      bucket[prop] = this[prop];
    });
    return bucket;
  }

  static getTable(name: string): Table {
    const table = new Table(name);
    table.name = name;
    const fields: Field[] = [];
    {
      const f = new Field("id", FieldType.Int);
      f.primaryKey = true;
      f.autoIncrement = true;
      fields.push(f);
    }
    {
      const f = new Field("username", FieldType.String);
      fields.push(f);
    }
    {
      const f = new Field("email", FieldType.String);
      fields.push(f);
    }
    {
      const f = new Field("password", FieldType.String);
      fields.push(f);
    }
    {
      const f = new Field("phone", FieldType.String);
      fields.push(f);
    }
    table.fields = fields;
    return table;
  }
}